package b2.widget.social
{
	import b2.config.*;
	import b2.net.*;
	import b2.player.*;
	import b2.util.*;
	import b2.widget.*;
	import b2.widget.tab.*;
	
	import flash.events.*;
	import flash.filters.*;
	import flash.text.*;
	
	public class SocialDialog extends Tab
	{
		public function SocialDialog()
		{
			var SocialDialogBgr:Class = ResourceHelper.getInstance().getClassByName("SocialDialogBgr");
			addChild(new SocialDialogBgr());
			super("", 1, 5, 1);
			this.setPagePos(16, 65);
			this.setPageSelectorPos(155, 372);
			//标题
			var titleText:TextField = new TextField();
			titleText.selectable = false;
        	titleText.autoSize = TextFieldAutoSize.CENTER;
        	var titleFomat:TextFormat = new TextFormat("微软雅黑", 16, 0xFFFFFF);
        	titleFomat.letterSpacing = 10;
			titleText.defaultTextFormat = titleFomat;
			titleText.filters = [new GlowFilter(0x000000, 1, 1.3, 1.3, 100, BitmapFilterQuality.HIGH)];
			titleText.text = "情报处";
			titleText.x = (this.width - titleText.width) / 2;
			titleText.y = 13;
			addChild(titleText);
			this.addEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
			this.addEventListener(Event.REMOVED_FROM_STAGE, onRemovedFromStage);
			//关闭按钮
			var CloseButton:Class = ResourceHelper.getInstance().getClassByName("CloseButton");
			var closeButton:BasicButton = new BasicButton(CloseButton);
			closeButton.x = this.width - closeButton.width - 20;
			closeButton.y = 15;
			closeButton.addEventListener(MouseEvent.CLICK, onCloseButtonClick);
			addChild(closeButton);
		}
		
		private function onAddedToStage(event:Event):void
		{
			ServerHelper.getInstance().getSocialInfo(true);
			ServerHelper.getInstance().addEventListener(ServerEvent.GET_SOCIAL_INFO, onGetSocialInfo);
		}

		private function onRemovedFromStage(event:Event):void
		{
			ServerHelper.getInstance().removeEventListener(ServerEvent.GET_SOCIAL_INFO, onGetSocialInfo);
		}
		
		private function onGetSocialInfo(event:Event):void
		{
			var me:PlayerInfo = PlayerInfoManager.getInstance().me;
			if (me.socialPackage != null)
			{
				for (var i:int = 0; i < me.socialPackage.socialList.length; i++)
				{
					var socialInfoItem:SocialInfoItem = new SocialInfoItem(me.socialPackage.socialList[i]);
					addItem(socialInfoItem, socialInfoItem.width, socialInfoItem.height);
				}
			}
		}
		
		private function onCloseButtonClick(event:MouseEvent):void
		{
			close();
		}
		
		public function close():void
		{
			if (this.parent != null)
				this.parent.removeChild(this);
		}
		
	}
}